#!/usr/bin/env bash

CPATH=$(cd `dirname $0`;pwd)
TOOLS="$CPATH/../lib"
source $TOOLS/lib.sh

if [[ $# -lt 4 ]];then
    echo "please specify package name usage: ./sync_pr_to_obs.sh <package-name> <obs_source_ip> <username> <password>"
    exit 1
fi

PACKAGE_NAME=$1
OBS_SOURCE_IP=$2
USERNAME=$3
PASSWORD=$4

sh ${TOOLS}/sshscp.sh -p "${PASSWORD}" -s "refresh_pr.sh" -d "${OBS_SOURCE_IP}:~"
sh ${TOOLS}/sshcmd.sh -c "./refresh_pr.sh ${PACKAGE_NAME}" -m "${OBS_SOURCE_IP}" -u ${USERNAME} -p ${PASSWORD}

delete_known_hosts()
{
    known_hosts=/root/.ssh/known_hosts
    > ${known_hosts}
}

function remotessh() {
    password=$1
    server=$2
    shift 2
    param="$@"
    expect -c "
        spawn ssh $server $param
        set timeout 120
        expect {
        \"*?es/no*\" {send \"yes\r\"; exp_continue}
        \"*?assword:*\" {send \"$password\r\" ;exp_continue }
        }
    "
}

cd $CPATH
# delete_known_hosts
# remotessh $4 172.16.1.89
# cd /root
#osc service remoterun openEuler:Extras $1
osc service remoterun openEuler:Mainline $1
if [ $? -ne 0 ]
then
    osc service remoterun bringInRely $1
    if [ $? -ne 0 ]
    then
        osc service remoterun openEuler:Extras $1
        if [ $? -ne 0 ]
        then
            echo "$1 jenkins gitee to obs error" | mail -s "$1 error" 303386771@qq.com
        fi
    fi
fi

osc service remoterun openEuler:20.03:LTS $1
if [ $? -ne 0 ]
then
    osc service remoterun openEuler:20.03:LTS:Extras $1
    if [ $? -ne 0 ]
    then
        osc service remoterun openEuler:20.03:LTS:performance $1
        if [ $? -ne 0 ]
        then
            echo "$1 jenkins gitee to obs error" | mail -s "$1 error" 303386771@qq.com
        fi
    fi
fi
